import java.util.Random;
import java.util.Scanner;
import java.util.HashSet;
import java.util.Set;

/**
 * 加减法口算练习系统 - V0.3
 * 生成无重复算式，确保加减法合理比例
 */
class Calculation_System_V0_3 {
    public static void main(String[] args) {
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("===== 加减法口算练习系统 - V0.3 =====");
        System.out.println("本次将生成50道无重复的加减法算式，确保加减法比例合理！\n");
        
        // 定义每行显示的算式数量
        int equationsPerLine = 2;
        int totalEquations = 50;
        
        // 使用HashSet存储已生成的算式，确保不重复
        Set<String> generatedEquations = new HashSet<>();
        
        // 创建存储算式和答案的数组
        String[] equations = new String[totalEquations];
        int[] answers = new int[totalEquations];
        int equationCount = 0;
        
        // 确保至少有一定数量的加法和减法算式
        int minAdditions = totalEquations / 2;  // 至少一半是加法
        int additionsCount = 0;
        
        // 生成50道不重复的算式
        while (equationCount < totalEquations) {
            boolean isAddition;
            
            // 确保加减法比例合理
            if (additionsCount < minAdditions) {
                // 还需要生成更多加法题
                isAddition = random.nextBoolean() || (equationCount < totalEquations - minAdditions);
            } else {
                // 加法题数量已达标，随机选择
                isAddition = random.nextBoolean();
            }
            
            int num1, num2, answer;
            String equationStr;
            
            // 尝试生成算式，直到生成一个唯一的算式
            boolean generated = false;
            while (!generated) {
                if (isAddition) {
                    // 加法：确保和不超过100
                    num1 = random.nextInt(99) + 1; // 1-99
                    num2 = random.nextInt(100 - num1) + 1; // 确保num1+num2 <= 100
                    answer = num1 + num2;
                    // 对于加法，考虑交换律，(a+b)和(b+a)视为相同算式
                    if (num1 > num2) {
                        int temp = num1;
                        num1 = num2;
                        num2 = temp;
                    }
                    equationStr = num1 + " + " + num2;
                } else {
                    // 减法：确保差不小于0
                    num2 = random.nextInt(99) + 1; // 1-99
                    num1 = random.nextInt(100 - num2) + num2; // 确保num1 >= num2
                    answer = num1 - num2;
                    equationStr = num1 + " - " + num2;
                }
                
                // 检查算式是否已存在
                if (!generatedEquations.contains(equationStr)) {
                    // 添加到已生成集合
                    generatedEquations.add(equationStr);
                    
                    // 构建完整的算式（包含序号和答案）
                    equations[equationCount] = (equationCount + 1) + ". " + equationStr + " = " + answer;
                    answers[equationCount] = answer;
                    
                    equationCount++;
                    if (isAddition) {
                        additionsCount++;
                    }
                    generated = true;
                }
            }
        }
        
        // 每行显示多个算式，保持整齐
        for (int i = 0; i < totalEquations; i += equationsPerLine) {
            StringBuilder line = new StringBuilder();
            
            for (int j = 0; j < equationsPerLine && i + j < totalEquations; j++) {
                // 格式化每个算式，确保宽度一致
                line.append(String.format("%-35s", equations[i + j]));
            }
            
            System.out.println(line.toString());
        }
        
        System.out.println("\n===== 算式生成完成 =====");
        System.out.println("共生成" + totalEquations + "道无重复的加减法算式。");
        System.out.println("其中加法算式：" + additionsCount + "道");
        System.out.println("其中减法算式：" + (totalEquations - additionsCount) + "道");
        System.out.println("\n实现说明：");
        System.out.println("1. 无重复算式：使用HashSet存储已生成的算式，确保不会生成重复题目");
        System.out.println("2. 加法考虑交换律：将a+b和b+a视为相同算式，避免重复");
        System.out.println("3. 合理比例：确保加法和减法算式数量相对平衡");
        System.out.println("4. 格式要求：加法和不超过100，减法差不小于0，每行显示多个算式");
        
        scanner.close();
    }
}